<template>
	<el-card>
		<!-- 搜索 -->
		<el-form v-show="showSearch" :model="searchData" inline ref="searchFormRef" id="searchForm">
			<el-form-item label="表名称" prop="tableName">
				<el-input v-model="searchData.tableName" clearable placeholder=""></el-input>
			</el-form-item>
			<el-form-item label="表描述" prop="tableComment">
				<el-input v-model="searchData.tableComment" clearable placeholder=""></el-input>
			</el-form-item>
			<el-form-item>
				<el-button icon="Search" type="primary" @click="searchClick">搜索</el-button>
				<el-button icon="Refresh" @click="resetClick">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row id="operate" style="margin-bottom: 3px;">
			<el-col :span="12">
				<el-button type="primary" plain icon="Plus" size="small" @click="addShowClick">新增</el-button>
			</el-col>
			<el-col :span="12">
				<right-toolbar :columns="columns" :search="true" v-model:showSearch="showSearch"
					@refresh="searchClick"></right-toolbar>
			</el-col>
		</el-row>

		<!-- 表格渲染 -->
		<gz-table ref="tableRef" :isColSet="true" :searchData="searchData" :columns="columns"
			api="appGenTableFindAllByPage">
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="300">
				<template v-slot="scope">
					<el-button text size="small" icon="View" @click="previewShowClick(scope.row)" style="margin-left: 0px;padding: 4px;">预览</el-button>
					<el-button text size="small" icon="Edit" @click="editShowClick(scope.row)" style="margin-left: 0px;padding: 4px;">编辑</el-button>
					<el-button text size="small" icon="Refresh" @click="tongbuClick(scope.row)" style="margin-left: 0px;padding: 4px;">同步</el-button>
					<el-button text size="small" icon="Download"
						@click="genTableClick(scope.row)" style="margin-left: 0px;padding: 4px;">生成代码</el-button>
					<el-button text type="danger" size="small" icon="Delete" @click="deleteClick(scope.row)" style="margin-left: 0px;padding: 4px;">删除</el-button>
				</template>
			</el-table-column>
		</gz-table>
		<ImportTable ref="importRef" @ok="importOk" />
		<EditTable ref="editTableRef" @ok="editTableOk" />
		<PreviewTable ref="previewTableRef" />
	</el-card>
</template>

<script>
	import ImportTable from "./ImportTable";
	import EditTable from "./EditTable";
	import PreviewTable from "./PreviewTable"
	export default {
		components: {
			ImportTable,
			EditTable,
			PreviewTable
		},
		data() {
			return {
				showSearch: true,
				//表格数据
				searchData: {
					tableName: null,
					tableComment: null,
				},
				curData: {
					name: null,
					imgUrl: null,
					state: "0",
					content: "",
					contentType: "1"
				},
				columns: [{
						label: "表名称",
						prop: "tableName",
						width: 150,
						isShow: true,
					},
					{
						label: "表描述",
						prop: "tableComment",
						isShow: true,
					},
					{
						label: "作者",
						prop: "author",
						isShow: true,
					},
					{
						label: "实体",
						prop: "className",
						width: 150,
						isShow: true,
					},
					{
						label: "生成包路径",
						prop: "packageName",
						type: "longText",
						width: 150,
						isShow: true,
					},
					{
						label: "实体类描述",
						prop: "classRemark",
						width: 150,
						isShow: true,
					},
					{
						label: "接口路径",
						prop: "jiekouPath",
						width: 150,
						isShow: true,
					},
					{
						label: "生成代码方式",
						prop: "genType",
						type: "tag",
						width: 150,
						isShow: true,
						options: [{
								type: "success",
								value: "0",
								label: "zip压缩包"
							},
							{
								type: "primary",
								value: "1",
								label: "自定义路径"
							}
						]
					},
					{
						label: "是否选择",
						prop: "tableIsSelect",
						width: 150,
						isShow: true,
						type: "tag",
						options: [{
								type: "success",
								value: "true",
								label: "是"
							},
							{
								type: "error",
								value: "false",
								label: "否"
							}
						]
					},
					{
						label: "备注",
						prop: "remark",
						type: "longText",
						width: 150,
						isShow: true,
					},
					{
						label: "更新时间",
						prop: "updateTime",
						width: 150,
						isShow: true,
					},
				],
			};
		},
		mounted() {
			this.$nextTick(() => {
				this.searchClick()
			})
		},
		methods: {
			//设置表格高度
			searchResize(data) {
				this.$refs.tableRef.refreshSize(data)
			},
			//搜索按钮
			searchClick() {
				this.$refs.tableRef.shuaxin(this.searchData)
			},
			//重置按钮
			resetClick() {
				this.$refs.searchFormRef.resetFields();
				this.searchClick()
			},
			//新增
			addShowClick() {
				this.$refs.importRef.show()
			},
			//编辑
			editShowClick(row) {
				this.$refs.editTableRef.show({
					tableId: row.id
				})
			},
			//添加/修改
			confirmClick() {
				let data = JSON.parse(JSON.stringify(this.curData))
				this.$refs.curFormRef.validate(async (valid) => {
					if (valid) {
						if (this.$common.isBlank(data.id)) {
							await this.$api.appGenTableAdd(data)
						} else {
							await this.$api.appGenTableEdit(data);
						}
						this.$refs.gzDialogRef.close()
						this.searchClick()
					}
				});
			},
			//删除
			deleteClick(row) {
				this.$common.msgBoxDel(() => {
					this.$api.appGenTableDelByIds(row.id).then(res => {
						this.searchClick();
					});
				})
			},
			genTableClick(row) {
				if (row.genType == "1") {
					this.$common.msgBox('确认要生成自定义路径吗？生成代码将覆盖原代码！！！', () => {
						this.$api.appGenTableGenCodeById(row.id).then(data => {
							this.$modal.msgSuccess("成功生成到自定义路径：" + row.genPath);
						});
					}, () => {})
				} else {
					this.$api.appGenTableDownloadByTableId({
						id: row.id,
						fileName: row.classRemark + ".zip"
					})
				}
			},
			importOk() {
				this.searchClick()
			},
			editTableOk(row) {
				this.searchClick()
			},
			/** 预览按钮 */
			previewShowClick(row) {
				this.$refs.previewTableRef.show({
					tableId: row.id
				})
			},
			/** 同步数据库操作 */
			tongbuClick(row) {
				this.$common.msgBox('确认要强制同步"' + row.tableName + '"表结构吗？', () => {
					this.$api.appGenTableTongbuById(row.id).then(data => {
						this.searchClick()
					})
				}, () => {})
			},
		},
	};
</script>

<style lang="scss" scoped>
	.el-form-item {
		padding-bottom: 0px;
		margin-bottom: 9px;
	}
</style>